package com.woniuxy.usercoachassemble.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniuxy.usercoachassemble.commons.ValidatorUtils;
import com.woniuxy.usercoachassemble.entity.CoachInfo;
import com.woniuxy.usercoachassemble.entity.User;
import com.woniuxy.usercoachassemble.entity.Userinfo;
import com.woniuxy.usercoachassemble.service.CoachInfoService;
import com.woniuxy.usercoachassemble.service.UserService;
import com.woniuxy.usercoachassemble.service.UserinfoService;
import org.example.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 上次登录地址 前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2020-07-09
 */
@RestController
@RequestMapping("userinfo")
public class UserinfoController {
@Autowired
    private UserinfoService userinfoServiceImpl;
    @Autowired
    private UserService userServiceImpl;
    @Autowired
    private CoachInfoService coachInfoServiceImpl;
  //更新学员的基本信息
    @RequestMapping("updatestudentinfo")
    public Result updateuserinfo(Userinfo userinfo){

        userinfoServiceImpl.updatauserinfobyuserid(userinfo);

        return  Result.Success();
    }
 //更新教练的基本信息
    @RequestMapping("updatecoahinfo")
    public Result updatecoachinfo(Userinfo  userinfo, CoachInfo coachInfo,int [] timeId){
       //头像地址
     if (ValidatorUtils.isNullOrEmpty(userinfo.getHeadPortrait()))
         userinfo.setHeadPortrait("默认头像");
 //昵称
        if (ValidatorUtils.isNullOrEmpty(userinfo.getNickname()))
            userinfo.setNickname("嘻嘻");
//地址
         if(ValidatorUtils.isNullOrEmpty(userinfo.getPreLoginAddress()))
            userinfo.setPreLoginAddress("未设置");
 //费用
        if(coachInfo.getCharge()==null)
            coachInfo.setCharge(0);
//空闲时间段 不选时教练全天空闲
        if(timeId.length==0){
            timeId[0]=1;
            timeId[0]=2;
            timeId[0]=3;
        }


        userinfoServiceImpl.updatecoachuserinfo(userinfo,coachInfo);
        return  Result.Success();

    }
//权限设置
    //update
    @RequestMapping("updateinfoprivilege")
public void  changeprivilegeofuserinfo(int userId,int infoLevel){
    UpdateWrapper<Userinfo> updateWrapper=new UpdateWrapper();
    updateWrapper.eq("user_id",userId).set("info_level",infoLevel);
    userinfoServiceImpl.update(updateWrapper);
}
//查询用户的所有信息
    @RequestMapping("getuserInfobyuserid")
public  Userinfo getuserInfobyuserid(HttpServletRequest request){
        Integer userId = Integer.parseInt(request.getHeader("userId"));
        Userinfo userinfo = userinfoServiceImpl.query().eq("user_id", userId).one();
        //基本信息
        User user = userServiceImpl.query().eq("user_id", userId).one();
        CoachInfo coachInfo = coachInfoServiceImpl.query().eq("user_id", userId).one();
        userinfo.setUser(user);
        userinfo.setCoachInfo(coachInfo);

        return  userinfo;
    }


}

